
#include "Ogre.h"

#include <CEGUI.h>
#include <CEGUIBase.h>
#include <CEGUIRenderer.h>
#include <CEGUITexture.h>

#include <OgreCEGUIRenderer.h>




#include <OIS/OIS.h>

#include "Main/Logger.h"
#include "GUI/DwarfGUI.h"
#include "GUI/Console.h"
#include "GUI/DFWindow.h"
#include "GUI/MapCellViewer.h"
#include "GUI/Cutaway.h"

#include "DwarfFort/Interface.h"


using namespace GUI;


DwarfGUI::DwarfGUI(Ogre::RenderWindow *window)
	:Loggable("GUI/DwarfGUI")
{
try
	{
	//	_asm int 3;
		mRenderer = new CEGUI::OgreCEGUIRenderer(window,Ogre::RENDER_QUEUE_OVERLAY,false,3000,
			Ogre::Root::getSingleton().getSceneManager("ExampleSMInstance"));

//		mScriptModule = new CEGUI::LuaScriptModule(/*Lugre::cScripting::GetSingletonPtr()->L*/);
		mSystem = new CEGUI::System(mRenderer);//,0,0,mScriptModule);
		CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"OgreDwarf.scheme");
		CEGUI::Logger::getSingleton().setLoggingLevel(CEGUI::Informative);
		mSystem->setDefaultFont((CEGUI::utf8*)"BlueHighway-10");
		mSystem->setDefaultMouseCursor((CEGUI::utf8*)"OgreDwarf", (CEGUI::utf8*)"MouseArrow");
		CEGUI::MouseCursor::getSingleton().setImage("OgreDwarf", "MouseMoveCursor");

		
		mSheet = CEGUI::WindowManager::getSingleton().loadWindowLayout((CEGUI::utf8*)"base.layout");
		
		mSystem->setGUISheet(mSheet);

		CEGUI::Window *si = CEGUI::WindowManager::getSingleton().getWindow("KillGame");
		si->subscribeEvent(CEGUI::Window::EventMouseClick,   CEGUI::Event::Subscriber(
			&DwarfGUI::killGame,this));
		
		
		mConsole = new Console(mSheet);
		mDFWindow = new DFWindow(mSheet,mRenderer);

		mMapCellViewer = new MapCellViewer(mSheet);
		mCutawayControler = new CutawayControler(mSheet);

		CEGUI::MouseCursor::getSingleton().setPosition(CEGUI::Point(0,0));
	}
	catch (Ogre::Exception e)
	{
			int i =0;
			_asm int 3;
	}
	catch (CEGUI::Exception e)
	{
		_asm int 3;
	}
}


DwarfGUI::~DwarfGUI()
{
	delete mCutawayControler;
	delete mMapCellViewer;
	delete mConsole;
	delete mDFWindow;
	if(mSheet)
	{
		CEGUI::WindowManager::getSingleton().destroyWindow(mSheet);
	}
	delete mSystem;
	delete mRenderer;
}


void DwarfGUI::update(float elapsedTime)
{
	mSystem->injectTimePulse(elapsedTime);
//	mMapCellViewer->update();
}




CEGUI::MouseButton DwarfGUI::convertButton(OIS::MouseButtonID buttonID)
{
    switch (buttonID)
    {
    case OIS::MB_Left:
        return CEGUI::LeftButton;

    case OIS::MB_Right:
        return CEGUI::RightButton;

    case OIS::MB_Middle:
        return CEGUI::MiddleButton;

    default:
        return CEGUI::LeftButton;
    }
}



bool DwarfGUI::keyPressed(const OIS::KeyEvent &arg)
{
	if (arg.key == OIS::KC_GRAVE)
	{
		mConsole->toggle();
		return true;
	}

	bool result = false;
	result |= mSystem->injectKeyDown(arg.key);
	result |= mSystem->injectChar(arg.text);  
	return result;
}	



bool DwarfGUI::keyReleased(const OIS::KeyEvent &arg)
{
	return mSystem->injectKeyUp(arg.key); 

}

bool DwarfGUI::mouseMoved(const OIS::MouseEvent &arg)
{
	
	//CEGUI::MouseCursor::getSingleton().show(); 
	return mSystem->injectMouseMove(arg.state.X.rel, arg.state.Y.rel);

}
bool DwarfGUI::mousePressed(const OIS::MouseEvent &arg,OIS::MouseButtonID id)
{
	return mSystem->injectMouseButtonDown(convertButton(id));
}
bool DwarfGUI::mouseReleased(const OIS::MouseEvent &arg,OIS::MouseButtonID id)
{
	return mSystem->injectMouseButtonUp(convertButton(id)); 
}

bool DwarfGUI::handleMouseMove(const CEGUI::EventArgs& e)
{
 return true;
}
//--------------------------------------------------------------------------
bool DwarfGUI::handleMouseButtonUp(const CEGUI::EventArgs& e)
{
	return true;
}

//--------------------------------------------------------------------------
bool DwarfGUI::handleMouseButtonDown(const CEGUI::EventArgs& e)
{
	return true;
}

void DwarfGUI::showCursor()
{
	CEGUI::MouseCursor::getSingleton().show();
}

void DwarfGUI::hideCursor()
{
	CEGUI::MouseCursor::getSingleton().hide();
}




// TEMPORARY function to quickly kill the game 

bool DwarfGUI::killGame(const CEGUI::EventArgs& e)
{
	exit(0);
}